srcdir =	@srcdir@
VPATH =		@srcdir@

prefix =	@prefix@
exec_prefix =	@exec_prefix@

bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/lib
incdir =	$(prefix)/include
guidedir =	$(prefix)/guide

INSTALL =	@INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@ -s
INSTALL_DATA =	@INSTALL_DATA@

SHELL =		/bin/sh
CC =		@CC@
AR =		ar
RM =		rm -rf
CP =		cp -a
MAKEINFO =	makeinfo
LHA =		lha -mraxe
MV =		mv
STRIP =		strip
TAR =		tar -vz
RANLIB =	@RANLIB@
AWK =		@AWK@
DEFS =		@DEFS@
LIBS =		@LIBS@
CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@
PROTECT =	/c/protect

# Used when building fd2inline, fd2inline.guide and the archives.
VERSION =	1.11

FD_DIR =	$(prefix)/os-lib/fd
OS_INC_DIR =	$(prefix)/os-include

OUR_CFLAGS =	$(DEFS) -Wall -Wstrict-prototypes '-DVERSION="$(VERSION)"' -resident $(CFLAGS)

# Pseudo-targets.
BUILD_INLINES =	stmp-built-inlines
BUILD_PRAGMAS =	stmp-built-pragmas
BUILD_PROTOS =	stmp-built-protos
BUILD_LIBS =	stmp-built-libs
BUILD_PPCINLINES =	stmp-built-ppcinlines
BUILD_PPCPRAGMAS =	stmp-built-ppcpragmas
BUILD_PPCPROTOS	 =	stmp-built-ppcprotos

# List of linker libraries to build. Each entry is INCBASE!LIBBASE (see below).
NONSTD_LIBS =	muimaster!mui ums!ums dospath!dospath \
		screennotify!screennotify wbstart!wbstart

# Archive names.
SRC_ARCHIVE_LHA = fd2inline-$(VERSION)-src.lha
SRC_ARCHIVE_TGZ = fd2inline-$(VERSION)-src.tgz
BIN_ARCHIVE =	fd2inline-$(VERSION)-bin.lha

# Used when building archives
TMP_DIR =	/tmp
TMP_SUBDIR =	fd2inline-$(VERSION)

# Flags to pass to submake.
FLAGS_TO_PASS =	"CC=$(CC)" \
		"AWK=$(AWK)" \
		"RM=$(RM)" \
		"AR=$(AR)" \
		"RANLIB=$(RANLIB)" \
		"OS_INC_DIR=$(OS_INC_DIR)" \
		"FD_DIR=$(FD_DIR)"

all:		fd2inline $(BUILD_INLINES) $(BUILD_PRAGMAS) $(BUILD_PROTOS) $(BUILD_LIBS) $(BUILD_PPCINLINES) $(BUILD_PPCPRAGMAS) $(BUILD_PPCPROTOS) guide

fd2inline:	fd2inline.c
		$(CC) $(OUR_CFLAGS) $(LDFLAGS) -o fd2inline $(srcdir)/fd2inline.c $(LIBS)
		if [ -f $(PROTECT) ]; then $(PROTECT) fd2inline +p; else true; fi

install:	all
		$(INSTALL_PROGRAM) fd2inline $(bindir)/fd2inline
		if [ -f $(PROTECT) ]; then \
			(cd $(bindir); $(PROTECT) fd2inline +p); \
		else true; \
		fi
		if [ -d $(incdir)/inline ]; then true; else mkdir $(incdir)/inline; fi
		$(INSTALL_DATA) $(srcdir)/include/inline-src/*.h $(incdir)/inline
		$(INSTALL_DATA) include/inline/*.h $(incdir)/inline
		if [ -d $(incdir)/pragmas ]; then true; else mkdir $(incdir)/pragmas; fi
		$(INSTALL_DATA) include/pragmas/*.h $(incdir)/pragmas
		if [ -d $(incdir)/proto ]; then true; else mkdir $(incdir)/proto; fi
		$(INSTALL_DATA) $(srcdir)/include/proto-src/*.h $(incdir)/proto
		$(INSTALL_DATA) include/proto/*.h $(incdir)/proto
		libs=`echo lib/lib*.a` ; if [ "lib/lib*.a" != "$${libs}" ]; then \
		  $(INSTALL_DATA) lib/lib*.a $(libdir); \
		else true; fi
		libs=`echo lib/libb/lib*.a` ; if [ "lib/libb/lib*.a" != "$${libs}" ]; then \
		  $(INSTALL_DATA) lib/libb/lib*.a $(libdir)/libb; \
		else true; fi
		libs=`echo lib/libb32/libm020/lib*.a` ; if [ "lib/libb32/libm020/lib*.a" != "$${libs}" ]; then \
		  $(INSTALL_DATA) lib/libb32/libm020/lib*.a $(libdir)/libb32/libm020; \
		else true; fi
		$(INSTALL_DATA) fd2inline.guide $(guidedir)

dist:		all
		# Create a temporary location.
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR) ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR); fi
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/include ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/include; fi
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/lib ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/lib; fi
		# Copy the source directory to it.
		( \
			cd $(srcdir); \
			$(CP) include/inline-src $(TMP_DIR)/$(TMP_SUBDIR)/include; \
			$(CP) include/proto-src $(TMP_DIR)/$(TMP_SUBDIR)/include; \
			$(CP) lib/Makefile.in lib/splitasm.awk $(TMP_DIR)/$(TMP_SUBDIR)/lib; \
			$(CP) manifests patches configure* COPYING fd2inline.c \
				fd2inline.texi install.sh Makefile.in \
				makepragmas.awk Product-Info \
				$(TMP_DIR)/$(TMP_SUBDIR) \
		)
		# Create the source archives.
		( \
			cd $(TMP_DIR); \
			$(LHA) a $(SRC_ARCHIVE_LHA) $(TMP_SUBDIR) \
		)
		$(RM) $(SRC_ARCHIVE_LHA)
		$(CP) $(TMP_DIR)/$(SRC_ARCHIVE_LHA) .
		$(RM) $(TMP_DIR)/$(SRC_ARCHIVE_LHA)
		$(TAR) -cf $(SRC_ARCHIVE_TGZ) -C $(TMP_DIR) $(TMP_SUBDIR)
		# Reorganize the temporary location.
		( \
			cd $(TMP_DIR)/$(TMP_SUBDIR)/include; \
			$(MV) inline-src inline; \
			$(MV) proto-src proto; \
			$(RM) ../lib/* \
		)
		# Copy the build directory to the temporary location.
		$(CP) include/inline include/pragmas include/proto $(TMP_DIR)/$(TMP_SUBDIR)/include
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/bin ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/bin; fi
		$(CP) fd2inline $(TMP_DIR)/$(TMP_SUBDIR)/bin
		$(STRIP) $(TMP_DIR)/$(TMP_SUBDIR)/bin/fd2inline
		if [ -f $(PROTECT) ]; then \
			(cd $(TMP_DIR)/$(TMP_SUBDIR)/bin; $(PROTECT) fd2inline +p); \
		else true; \
		fi
		$(CP) lib/lib*.a $(TMP_DIR)/$(TMP_SUBDIR)/lib
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb; fi
		$(CP) lib/libb/lib*.a $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb32 ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb32; fi
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb32/libm020 ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb32/libm020; fi
		$(CP) lib/libb32/libm020/lib*.a $(TMP_DIR)/$(TMP_SUBDIR)/lib/libb32/libm020
		if [ -d $(TMP_DIR)/$(TMP_SUBDIR)/guide ]; then true; else mkdir $(TMP_DIR)/$(TMP_SUBDIR)/guide; fi
		$(CP) fd2inline.guide $(TMP_DIR)/$(TMP_SUBDIR)/guide
		# Create the binary archive.
		( \
			cd $(TMP_DIR); \
			$(LHA) a $(BIN_ARCHIVE) $(TMP_SUBDIR)/ bin guide include lib COPYING \
		)
		$(RM) $(BIN_ARCHIVE)
		$(CP) $(TMP_DIR)/$(BIN_ARCHIVE) .
		$(RM) $(TMP_DIR)/$(BIN_ARCHIVE)
		$(RM) $(TMP_DIR)/$(TMP_SUBDIR)

$(BUILD_INLINES):	fd2inline
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/inline ]; then true; else mkdir include/inline; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ "$${rootname}" = "mathffp" -o "$${rootname}" = "mathtrans" ]; then \
				continue; \
			fi; \
			echo "making include/inline/$$rootname.h..."; \
			./fd2inline $$fd $(OS_INC_DIR)/clib/$${rootname}_protos.h -o include/inline/$$rootname.h; \
		done
		touch $(BUILD_INLINES)

$(BUILD_PPCINLINES):	fd2inline
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/ppcinline ]; then true; else mkdir include/ppcinline; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ "$${rootname}" = "mathffp" -o "$${rootname}" = "mathtrans" ]; then \
				continue; \
			fi; \
			echo "making include/ppcinline/$$rootname.h..."; \
			./fd2inline $$fd $(OS_INC_DIR)/clib/$${rootname}_protos.h -powerup -o include/ppcinline/$$rootname.h; \
		done
		touch $(BUILD_PPCINLINES)

$(BUILD_PRAGMAS):	$(srcdir)/makepragmas.awk
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/pragmas ]; then true; else mkdir include/pragmas; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ "$${rootname}" = "mathffp" -o "$${rootname}" = "mathtrans" ]; then \
				continue; \
			fi; \
			echo "making include/pragmas/$${rootname}_pragmas.h..."; \
			$(AWK) -f $(srcdir)/makepragmas.awk -v PRAGMAS=$$rootname >include/pragmas/$${rootname}_pragmas.h; \
		done
		touch $(BUILD_PRAGMAS)

$(BUILD_PPCPRAGMAS):	fd2inline
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/ppcpragmas ]; then true; else mkdir include/ppcpragmas; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ "$${rootname}" = "mathffp" -o "$${rootname}" = "mathtrans" ]; then \
				continue; \
			fi; \
			echo "making include/ppcpragmas/$${rootname}_pragmas.h..."; \
			./fd2inline --pragma --powerup $$fd $(OS_INC_DIR)/clib/$${rootname}_protos.h -o include/ppcpragmas/$${rootname}_pragmas.h; \
		done
		touch $(BUILD_PPCPRAGMAS)

$(BUILD_PROTOS):	fd2inline
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/proto ]; then true; else mkdir include/proto; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ -f $(srcdir)/include/proto-src/$$rootname.h ]; then \
				continue; \
			fi; \
			echo "making include/proto/$$rootname.h..."; \
			./fd2inline --proto $$fd -o include/proto/$$rootname.h; \
		done
		touch $(BUILD_PROTOS)

$(BUILD_PPCPROTOS):	fd2inline
		if [ -d include ]; then true; else mkdir include; fi
		if [ -d include/ppcproto ]; then true; else mkdir include/ppcproto; fi
		for fd in $(FD_DIR)/*.fd; do \
			rootname=`basename $$fd .fd | sed "s:_lib$$::"`; \
			if [ -f $(srcdir)/include/proto-src/$$rootname.h ]; then \
				continue; \
			fi; \
			echo "making include/ppcproto/$$rootname.h..."; \
			./fd2inline --proto $$fd --powerup -o include/ppcproto/$$rootname.h; \
		done
		touch $(BUILD_PROTOS)

$(BUILD_LIBS):	fd2inline
		for bases in $(NONSTD_LIBS); do \
			incbase=`echo $$bases | sed "s:!.*$$::"`; \
			libbase=`echo $$bases | sed "s:^.*!::"`; \
			if [ -f include/inline/$$incbase.h ]; then \
				echo "making lib$$libbase.a..."; \
				(cd lib; $(MAKE) $(FLAGS_TO_PASS) INCBASE=$$incbase LIBBASE=$$libbase); \
			else true; fi; \
		done
		touch $(BUILD_LIBS)

guide:		fd2inline.guide

fd2inline.guide:	$(srcdir)/fd2inline.texi
		if [ -f version.texi ]; then true; else echo "@set version $(VERSION)" >version.texi; fi
		$(MAKEINFO) $(srcdir)/fd2inline.texi -o fd2inline.guide

# Make a linker library with stubs, if for some reason one can't use
# inline headers. This requires two arguments on "make" commandline:
#
# INCBASE: base name of the includes (muimaster for MUI etc).
# LIBBASE: base name of the library (mui for MUI etc).

alllib:		fd2inline
		if [ "$(INCBASE)" = "" -o "$(LIBBASE)" = "" ]; then \
			echo "Require two arguments: INCBASE and LIBBASE"; \
		else \
			(cd lib; $(MAKE) $(FLAGS_TO_PASS) INCBASE=$(INCBASE) LIBBASE=$(LIBBASE)); \
		fi

clean:
		$(RM) version.texi *.o *~ *!
		(cd lib; $(MAKE) $(FLAGS_TO_PASS) clean)

clobber:	clean
		$(RM) fd2inline fd2inline.guide \
			include/inline include/pragmas include/proto \
			$(BUILD_INLINES) $(BUILD_PRAGMAS) $(BUILD_PROTOS) $(BUILD_LIBS) \
			$(SRC_ARCHIVE_LHA) $(SRC_ARCHIVE_TGZ) $(BIN_ARCHIVE)
		(cd lib; $(MAKE) $(FLAGS_TO_PASS) clobber)
